package com.wk.code.wkserver.controller.common;


import com.wk.code.wkserver.response.result.R;
import com.wk.code.wkserver.service.IUploadService;
import com.wk.code.wkserver.utils.QiniuUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.BindException;
import java.rmi.RemoteException;
import java.util.List;

/**
 * @author wq
 * @version 1.0
 * @date 2021/3/25 22:54
 * 七牛云文件上传管理
 */
@RestController
@Slf4j
@RequestMapping("/upload/v1")
@Api(tags = "七牛云文件上传管理")
public class QiniuUploadController {


    /**
     * 上传图片
     *
     * @param multipartFile multipartFile
     * @return R
     */
    @ApiOperation(value = "上传图片", notes = "上传图片")
    @PostMapping(value = "/img")
    private R<String> upload(@RequestParam(value = "file", required = false) MultipartFile multipartFile) throws IOException {
        if (multipartFile.isEmpty()) {
            throw new RemoteException("请选择文件");
        }
        FileInputStream inputStream = (FileInputStream) multipartFile.getInputStream();
        String key = System.currentTimeMillis() + ".jpg";
        String path = QiniuUtils.uploadQNImg(inputStream, key);
        log.info("path===>" + path);
        return R.success(path);
    }
}
